package game;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;

public class SelecaoMenu
{
	
	private boolean movimentoLateral;
	private Point[] pos;
	private Point size;
	private boolean podeClicar = true;
	private Point position;
	private int indexOpcao = 0;
	private Image image;
	private float time = 0;
	private float delayClique = 0.2f;
	private Integer numColunas = null;
	
	
	public int getIndexOpcao() {
		return indexOpcao;
	}

	
	public SelecaoMenu(Point[] pos, Point size, boolean movimentacaoLateral)
	{
		this.pos = pos;
		this.size = size;
		this.movimentoLateral = movimentacaoLateral;
		this.position = this.pos[0];
		this.image = LoadImages.getImage(76);
	}
	
	public SelecaoMenu(Point[] pos, Point size, int numColunas, int numLinhas)
	{
		this.pos = pos;
		this.size = size;
		this.numColunas = numColunas;
		this.position = this.pos[0];
		this.image = LoadImages.getImage(76);
	}
	
	public void update()
	{
		time -= Time.getInstance().getLoopTime();
		if(time <= 0)
			podeClicar = true;
		else
			podeClicar = false;
		
		if(numColunas == null)
		{
			if(this.podeClicar)
			{
				if(movimentoLateral)
					this.MovimentoLateral();
				else
					this.MovimentoVertical();
			}
		}
		else
		{
			if(this.podeClicar)
				Movimento();
		}
		
		position = this.pos[this.indexOpcao];
		//System.out.println(this.indexOpcao);
		
	}
	
	public void draw(Graphics2D g2d)
	{
		g2d.drawImage(image, this.position.x, this.position.y, this.size.x, this.size.y, null);
	}
	
	private void MovimentoLateral()
	{
		if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_LEFT))
		{
			this.indexOpcao--;
			time = delayClique;
		}
		else if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_RIGHT))
		{
			this.indexOpcao++;
			time = delayClique;
		}
		
		if(this.indexOpcao < 0)
			this.indexOpcao = this.pos.length - 1;
		else if(this.indexOpcao > this.pos.length - 1)
			this.indexOpcao = 0;
	}
	
	private void MovimentoVertical()
	{
		if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_UP))
		{
			this.indexOpcao--;
			time = delayClique;
		}
		else if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_DOWN))
		{
			this.indexOpcao++;
			time = delayClique;
		}

		if(this.indexOpcao < 0)
			this.indexOpcao = this.pos.length - 1;
		else if(this.indexOpcao > this.pos.length - 1)
			this.indexOpcao = 0;
	}

	private void Movimento()
	{
		if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_LEFT))
		{
			if(this.indexOpcao%this.numColunas == 0)
				this.indexOpcao += numColunas-1;
			else 
				this.indexOpcao--;
			if(this.indexOpcao == this.pos.length)
				this.indexOpcao--;
			time = delayClique;
		}
		else if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_RIGHT))
		{
			if(this.indexOpcao%this.numColunas == 2)
				this.indexOpcao -= numColunas-1;
			else 
				this.indexOpcao++;
			
			if(this.indexOpcao == this.pos.length)
				this.indexOpcao	-= 2;
			time = delayClique;
		}
		else if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_UP))
		{
			if(this.indexOpcao == 0)
				this.indexOpcao = 27;
			else if(this.indexOpcao == 1)
				this.indexOpcao = 28;
			else if(this.indexOpcao == 2)
				this.indexOpcao = 26;
			else
				this.indexOpcao -= this.numColunas;
			
			time = delayClique;
		}
		else if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_DOWN))
		{
			if(this.indexOpcao == 26)
				this.indexOpcao = 2;
			else if(this.indexOpcao == 28)
				this.indexOpcao = 1;
			else if(this.indexOpcao == 27)
				this.indexOpcao = 0;
			else
				this.indexOpcao += this.numColunas;
			
			time = delayClique;
		}
		
	}
	
}
